﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using Utils.Share;

namespace Utils.EasyUI.Results
{
    /// <summary>
    ///     EasyUi提交表单返回结果
    /// </summary>
    public class EasyUiResult
    {
        /// <summary>
        ///     状态码
        /// </summary>
        private readonly StateCode _code;

        /// <summary>
        ///     数据
        /// </summary>
        private readonly IEnumerable<object> _data;

        /// <summary>
        ///     消息
        /// </summary>
        private readonly string _message;

        /// <summary>
        ///     初始化EasyUi提交表单返回结果
        /// </summary>
        /// <param name="code">状态码</param>
        /// <param name="message">消息</param>
        /// <param name="data">数据</param>
        public EasyUiResult(StateCode code, string message, IEnumerable<object> data = null)
        {
            _code = code;
            _message = message;
            _data = data;
        }

        /// <summary>
        ///     获取结果
        /// </summary>
        public ActionResult GetResult()
        {
            return new ContentResult {Content = ToString()};
        }

        /// <summary>
        ///     输出结果
        /// </summary>
        public override string ToString()
        {
            var value = _code.ToStr();
            if (string.IsNullOrWhiteSpace(value))
                throw new ArgumentNullException("member");
            var cvalue = (int) Enum.Parse(_code.GetType(), _code.ToString(), true);

            return Json.ToJson(new {Code = cvalue, Message = _message, Data = _data});
        }
    }
}